Page({
  data: {
    passportInfo: {},
    encryptedData: '',
  },
  onShareAppMessage() {
    return {
      title: '查看通行证',
      path: `/visitor_pkg/pages/passport/index?encryptedData=${this.data.passportInfo.encryptedData}`,
      imageUrl: this.data.passportInfo.url,
    }
  },
  // 保存二维码
  saveImage() {
    wx.downloadFile({
      url: this.data.passportInfo.url,
      success: res => {
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success: res => (
            wx.utils.toast("保存成功")
          )
        })

      }
    })

  },

  async onLoad({
    id,
    encryptedData
  }) {

    id && await this.getPassportInfo(id);

    encryptedData && this.setData({
      encryptedData
    });
    encryptedData && await this.getSharePassportInfo(encryptedData)
  },

  // 获取通行证详情信息
  async getPassportInfo(id) {
    const {
      data
    } = await wx.http.get(`/visitor/${id}`)
    this.setData({
      passportInfo: {
        ...data
      }
    })
  },

  // 获取分享通行证的详情信息
  async getSharePassportInfo(encryptedData) {
    const {
      data
    } = await wx.http.get(`/visitor/share/${encryptedData}`)
    this.setData({
      passportInfo: {
        ...data
      }
    })
  }
})